package com.bone.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.bone.util.ServletUtil;
import com.bone.util.StringUtil;
import com.bone.workstation.po.Employee;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport {	
	/**
	 * 
	 */
	private static final long serialVersionUID = -209605848524004148L;
	/**
	 * Pagination
	 */
	protected Pagination pagination;
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	
	public String getPath() {
		String basePath = this.getRequest().getContextPath();
		String path = getRequest().getScheme() + "://"
				+ getRequest().getServerName() + ":" + getRequest().getServerPort()
				+ basePath + "/";
		return path;
	}
	
	
	/**
	 * 初始化当前页和Pagination对象
	 */
	protected Pagination initPagination(String currentPage) {
		// request url method
		int page = 1;
		if (currentPage != null && !"".equals(currentPage)) {
			try {
				page = Integer.parseInt(currentPage);
			} catch (Exception e) {
			    int a = currentPage.indexOf(",");
				String currpage = currentPage.substring(0, a);
				if(!StringUtil.isEmptyString(currpage)){
					page = Integer.parseInt(currpage);
				}
			}
		}
		// 构建新的Pagination对象
		pagination = new Pagination();
		int iperPageRecord = pagination.getPerPageRecord();
		 
		if(iperPageRecord!=pagination.getPerPageRecord()){
			pagination.setPerPageRecord(iperPageRecord);
			pagination.setPage(page);
		}else{
			pagination.setPage(page);	
		}				
		return pagination;
	}

	
	/**
	 * 发送Ajax请求结果
	 * 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void sendAjaxResult(String json) {

		this.getResponse().setContentType("text/html;charset=UTF-8");
		this.getResponse().setCharacterEncoding("UTF-8");
		try {
			PrintWriter out = this.getResponse().getWriter();
			out.write(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送Ajax请求结果json
	 * 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void sendAjaxResultByJson(String json) {
		this.getResponse().setContentType("application/json;charset=UTF-8");
		this.getResponse().setCharacterEncoding("UTF-8");
		try {
			PrintWriter out = this.getResponse().getWriter();
			out.write(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void sendAjaxMsg(String msg){
		try {
			this.getResponse().getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	protected String getMethod() {
		return getRequest().getMethod();
	}
	
	@SuppressWarnings("unchecked")
	protected Map<String, Object> getParameters() {
		return getRequest().getParameterMap();
	}
	
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	protected String getRequestParameter(String key) {
		return getRequest().getParameter(key);
	}
	protected ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}
	
	protected void saveMessage(String msg) {
		getRequest().setAttribute("messages", msg);
	}

	protected void errorMessage(String msg) {
		getRequest().setAttribute("errorMessages", msg);
	}
	
	public Cookie[] getCookies() {
		return ServletActionContext.getRequest().getCookies();
	}
	 
	
	public Map<String, String> getPureParaPair() {
		Map map = ServletActionContext.getRequest().getParameterMap();
		Map<String, String> result = new HashMap<String, String>();
		for (String key : (Set<String>)map.keySet()) {
			String[] values = (String[])map.get(key);
			result.put(key, values[0]);
			log.info(key + " = " + values[0]);
		}
		return result;
	}
	
	protected void setRequestAttribute(String key, Object value){
		getRequest().setAttribute(key, value);
	}
	
	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	protected void putSession(String key, Object obj) {
		ServletUtil.putSession(getRequest(), getResponse(), key, obj);
	}
	
	protected Object getSession(String key) {
		return ServletUtil.getSession(getRequest(), getResponse(), key);
	}
	
	protected void removeSession(String key){
		ServletUtil.removeSession(getRequest(), getResponse(), key);
	}
	
	
	/**
	 * 获取当前登录用户
	 * @return
	 */
	public Employee getSessionUser(){
		Employee employee = (Employee)getSession().getAttribute("employee");
		if (employee!=null) {
			return employee;
		}else{
			return new Employee();
		}
	}
	
	/**
	 * 获得session中的UserName
	 * 
	 * @return
	 */
	public String getSessionEmployeeName() {
		return getSessionUser().getName();
	}
	
	/**
	 * 获得session中的UserName
	 * @return
	 */
	public Long getOperatorId() {
		return getSessionUser().getEmployeeId();
	}
	
	/**
	 * 获得session中的UserName
	 * @return
	 */
	public String getOperatorName() {
		return getSessionUser().getName();
	}
	
	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	
}
